package com.fz.controller.member;

import com.fz.aop.login.AuthClient;
import com.fz.common.Global;
import com.fz.common.GlobalAll;
import com.fz.common.ResponseEntity;
import com.fz.dto.client.login.LoginMcDto;
import com.fz.dto.client.login.LoginMpDto;
import com.fz.dto.client.login.WxAuthDto;
import com.fz.entity.member.Member;
import com.fz.security.utils.SecurityUtils;
import com.fz.service.member.LoginService;
import com.fz.utils.SendMsgUtils;
import com.fz.vo.client.login.ClientLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "登录管理", tags = "登录管理")
@RequestMapping("/login")
@ApiSort(value = 1)
@CrossOrigin
@SuppressWarnings("unchecked")
public class LoginController {


    @Resource
    private LoginService loginService;
    @Autowired
    private SendMsgUtils sendMsgUtils;

    /**
     * @方法描述: 手机号验证码注册/登录
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/2/26
     */
    @PostMapping("/loginByMc")
    @ApiOperation(value = "手机号短信验证码登录")
    @ApiOperationSupport(order = 1)
    public ResponseEntity<ClientLoginVo> loginByMc(HttpServletRequest request, HttpServletResponse response,
                                                   @RequestBody LoginMcDto loginMcDto) throws Exception {
        return loginService.loginMc(loginMcDto.getMobilePhone(),loginMcDto.getCode(),request,response);
    }


    /**
     * @方法描述: 手机号密码登录
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/2/26
     */
    @PostMapping("/loginByMP")
    @ApiOperation(value = "手机号密码登录")
    @ApiOperationSupport(order = 2)
    public ResponseEntity<ClientLoginVo> loginByMp(HttpServletRequest request, HttpServletResponse response,
                                                   @RequestBody LoginMpDto loginMpDto) throws Exception{
        return loginService.loginByMp(loginMpDto.getMobilePhone(),loginMpDto.getPassword(),request,response);
    }


//    @GetMapping("/getOpenidAndUnionid")
//    @ApiOperation(value = "获取openid", notes = "获取openid和unionid")
//    public ResponseEntity<AppletVo> getOpenidAndUnionid(String encryptedData, String code, String iv)throws Exception{
//        return loginService.getOpenidAndUnionid(encryptedData,code,iv);
//    }


    @PostMapping("/loginByWechat")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "微信授权登录/微信小程序授权登录")
    public ResponseEntity<ClientLoginVo> loginWechat(@RequestBody WxAuthDto wxAuthDto,HttpServletRequest request, HttpServletResponse response) throws Exception{
        return loginService.loginByWechat(wxAuthDto,request,response);
    }

    @GetMapping("/sendMsgCode")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "发送注册/登录验证码", notes = "发送注册/登录验证码")
    public ResponseEntity sendMsgCode(String mobilePhone)throws Exception{
        return sendMsgUtils.sendSms(mobilePhone,Global.LOGIN_MSG_CODE_KEY + mobilePhone,Global.LOGIN_MSG_CODE_KEY_TIME,GlobalAll.LOGIN_MSG_TYPE);
    }

    @PostMapping("/logout")
    @ApiOperation(value = "登出")
    @AuthClient
    @ApiOperationSupport(order = 5)
    public ResponseEntity logout(HttpServletRequest request) throws Exception {
        String token = SecurityUtils.getToken();
        return loginService.logout(token,request);
    }


    @GetMapping("/test")
    @ApiOperation(value = "测试")
    @AuthClient
    @ApiOperationSupport(order = 6)
    public ResponseEntity test(HttpServletRequest request) throws Exception {
        String token = SecurityUtils.getToken();
        System.out.println("token：" + token);
        Member member = SecurityUtils.getCurrentUser();
        System.out.println("member：" + member.getUid());
        return ResponseEntity.buildSuccess();
    }
}
